﻿using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace IPR_WebApi.Controllers
{
    public class CameraVideoController : ApiController
    {
        [HttpGet]
        [Route("api/CameraVideo/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                swhere = swhere ?? string.Empty;
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                if (!string.IsNullOrWhiteSpace(swhere))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    swhere = " 1=1 ";
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "VideoName")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND VideoName like '%{item.Value}%' ";
                        }
                        if (item.Key == "CourseName")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND TCOName like '%{item.Value}%' ";
                        }
                        
                        if (item.Key == "cheduiList")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND d.DepID in ({item.Value}) ";
                        }
                        if (item.Key == "PlanName")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND TPName like '%{item.Value}%' ";
                        }
                        if (item.Key == "StartDateTime")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND VideoEndDateTime>='{item.Value}' ";
                        }
                        if (item.Key == "EndDateTime")
                        {
                            if ((item.Value != "") && (item.Value != null))
                                swhere += $" AND VideoEndDateTime<='{item.Value}' ";
                        }
                    }
                }
                bool blExist = false;
                int rowCount = 0;
                
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.CameraVideo().GetListByPage(swhere, "AddDateTime desc", startindex, endindex);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
        [HttpPost]
        [Route("api/CameraVideo/Edit")]
        public IHttpActionResult UpdateDep(IPR_StudyMeetingMrgSys.Model.CameraVideo model)
        {
            ResultMsg resultMsg = new ResultMsg();
            IPR_StudyMeetingMrgSys.Model.CameraVideo MolDep = new IPR_StudyMeetingMrgSys.Model.CameraVideo();
            MolDep.AutoID = model.AutoID;
            MolDep.VideoName = model.VideoName;
            if (new IPR_StudyMeetingMrgSys.BLL.CameraVideo().Update(MolDep))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新部门信息失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        [HttpGet]
        [Route("api/CameraVideo/SearchForTCOID")]
        public IHttpActionResult SearchForTCOID(string TCOID)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                bool blExist = false;
                int rowCount = 1;

                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.CameraVideo().GetList(" TCOID="+TCOID);
                if (dsSearch != null)
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = rowCount;
                            blExist = true;
                        }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText() + exc.Message;
                resultMsg.Data = "查询异常！" + exc.Message;
            }
            return Json(resultMsg);
        }
    }
}
